package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kang.entity.SysUser;
import com.kang.service.LoginService;
import com.kang.util.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailImpl implements ReactiveUserDetailsService {

    @Autowired
    private LoginService loginService;

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        //查询用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        SysUser user = loginService.getOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名或者密码错误");
        }
        //TODO 查询对应权限信息
        //ArrayList<String> permission = new ArrayList<>(Arrays.asList("test","admin"));
        //List<String> list = menuMapper.selectPermsByUserId(user.getId());

        return Mono.just(new LoginUser(user,null));
    }
}
